#region usings
using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Data;

using CStock;
#endregion



namespace CapitalReSources
{
	/// <summary>
	/// Summary description for DataSetClass.
	/// </summary>
	public class DataGridClass : IDataGridClass
	{
		private IMainClass mainClass;

		private IProgramContext programContext;

		/// <summary>
		/// 
		/// </summary>
		private DataGrid dataGrid;
		/// <summary>
		/// 
		/// </summary>
		private DataSet dataSet;
		/// <summary>
		/// 
		/// </summary>
		private string path;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="myDataGrid"></param>
		/// <param name="myDataSet"></param>
		/// <param name="myPath"></param>
		public DataGridClass(ref DataGrid myDataGrid,DataSet myDataSet,string myPath)
		{
			dataGrid = myDataGrid;
			dataSet = myDataSet;
			path = myPath;
			
		}
		/// <summary>
		/// 
		/// </summary>
		public DataGridClass()
		{
			this.mainClass = MainClass.Instance;
			
			this.programContext = this.mainClass.ProgramContext;
			this.dataSet = this.programContext.TradeEconomy.Portfolio_Selected.DataSet;
			path = this.programContext.Configuration.Directories.ApplicationPath;
			
			
		}
		

		/// <summary>
		/// 
		/// </summary>
		public DataGrid DataGrid
		{
			get { 
				return dataGrid; 
			}
			set 
			{
				dataGrid= value ; 
			}
		}

		/// <summary>
		/// dataholder - the main source of data
		/// </summary>
		public IProgramContext ProgramContext
		{
			get
			{
				return this.programContext;
			}
			set
			{
				this.programContext = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void SetLargeDefaultView()
		{
			if(dataGrid.TableStyles.Count > 0)
				dataGrid.TableStyles.Remove(dataGrid.TableStyles[dataGrid.TableStyles.Count-1]);


			DataGridTableStyle ts1 = new DataGridTableStyle();
			ts1.MappingName = dataSet.Tables[0].TableName;
			ts1.AlternatingBackColor = Color.LightGray;

      
			DataGridColumnStyle boolCol = new DataGridBoolColumn();
			for(int i=0;i< dataSet.Tables[0].Columns.Count;i++)
			{
				DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
				TextCol.MappingName = dataSet.Tables[0].Columns[i].ColumnName ;
				TextCol.HeaderText = dataSet.Tables[0].Columns[i].ColumnName ;
				TextCol.Width = 150;
				ts1.GridColumnStyles.Add(TextCol);

			}

			dataGrid.TableStyles.Add(ts1);
			
		}
		/// <summary>
		/// 
		/// </summary>
		public void SimpleDefaultGrid()
		{

			if(dataGrid.TableStyles.Count > 0)
				dataGrid.TableStyles.Remove(dataGrid.TableStyles[dataGrid.TableStyles.Count-1]);

			DataGridTableStyle ts1 = new DataGridTableStyle();
			ts1.MappingName = dataSet.Tables[0].TableName;
			ts1.AlternatingBackColor = Color.LightGray;

			for(int i=0;i< 2;i++)
			{
				DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
				TextCol.MappingName = dataSet.Tables[0].Columns[i].ColumnName ;
				TextCol.HeaderText = dataSet.Tables[0].Columns[i].ColumnName ;
				TextCol.Width = 150;
				ts1.GridColumnStyles.Add(TextCol);

			}
			for(int i= (dataSet.Tables[0].Columns.Count - 3 );i< dataSet.Tables[0].Columns.Count; i++)
			{
				DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
				TextCol.MappingName = dataSet.Tables[0].Columns[i].ColumnName ;
				TextCol.HeaderText = dataSet.Tables[0].Columns[i].ColumnName ;
				TextCol.Width = 150;
				try
				{
					if(! ts1.GridColumnStyles.Contains(TextCol))
						ts1.GridColumnStyles.Add(TextCol);
				}
				catch
				{
					//this style is already there, so : do nothing -
				}

			}

			dataGrid.TableStyles.Add(ts1);
			
		}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="myColumn"></param>
        //public void RemoveSecondColumn( )
        //{
			
        //    int columnIndex = 1;
        //    // remove the second column
        //    GridColumnStylesCollection myGridColumns;
        //    myGridColumns = dataGrid.TableStyles[0].GridColumnStyles;
        //    myGridColumns.RemoveAt( columnIndex );
        //    dataGrid.Refresh();

        //    return ;
        //}
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public bool SaveOptionsGrid()
		{
			System.Data.DataSet MyDataSet= new System.Data.DataSet("DataGridOptions");
			MyDataSet.Namespace= "NetFrameWork";
			System.Data.DataTable MyTable= new System.Data.DataTable("DataGridColumn");

			if(dataGrid.TableStyles.Count <1)
				return false;
			
			//System.Windows.Forms.DataGridColumnStyle
			DataGridTableStyle myTableStyle = dataGrid.TableStyles[0];

			
			DataColumn c1 = new DataColumn("Mapping Name");
			MyTable.Columns.Add(c1);
			c1 = new DataColumn("Header Text");
			MyTable.Columns.Add(c1);
			c1 = new DataColumn("Width");
			MyTable.Columns.Add(c1);


			//, Type.GetType("System.Int32")
		
			MyDataSet.Tables.Add(MyTable);
		
			
			try	
			{	
				int iNum =myTableStyle.GridColumnStyles.Count;
			  
				for(int i=0;i < iNum ;i++)
				{
					DataRow newRow= MyTable.NewRow();
					DataGridColumnStyle myGridColumnStyle = myTableStyle.GridColumnStyles[i];
				
					string strValue=myGridColumnStyle.MappingName; 	
					newRow[0]= strValue;								
					strValue=myGridColumnStyle.HeaderText; 	
					newRow[1]= strValue;
					strValue=myGridColumnStyle.Width.ToString(); 		
					newRow[2]= strValue;					
	

					MyTable.Rows.Add(newRow);
				}
				
			
				//writer.Flush();
				//writer.Close();
          

				
			}
			catch ( System.Exception e1 )
			{
				throw e1 ;
			}
			
			MyDataSet.AcceptChanges();
	
			string strFile=path  + "\\Options\\DataGridOptions.xml";
			CUtil.XMLAccessData MyXMLAcess = new CUtil.XMLAccessData(strFile);
			if(MyXMLAcess.WriteXML(MyDataSet))
				return true;	
			else
				return false;

		
		}
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public bool ReadOptionsDataGrid()
		{
			string strFile=path  + "\\Options\\DataGridOptions.xml";

			CUtil.XMLAccessData MyXMLAcess = new CUtil.XMLAccessData(strFile);
			//m_arrSharesALL= new System.Collections.ArrayList();
			DataTable MyDataTable=null;
			DataSet MyDataSet=null;
			if (!MyXMLAcess.ReadXMLFile(ref MyDataTable, ref MyDataSet))
				return false;

			if (MyDataTable.Rows.Count <= 0)
				return false;

			string MappingName=" ";
			string HeaderText= " ";
			string Width = " ";


			//grid
			if(dataGrid.TableStyles.Count > 0)
				dataGrid.TableStyles.Remove(dataGrid.TableStyles[dataGrid.TableStyles.Count-1]);

			DataGridTableStyle ts1 = new DataGridTableStyle();
			ts1.MappingName = dataSet.Tables[0].TableName;
			ts1.AlternatingBackColor = Color.LightGray;

      
		

			//-------------------------
			DataGridColumnStyle boolCol = new DataGridBoolColumn();
			foreach (DataRow dr in MyDataTable.Rows)
			{	
				MappingName = dr[0].ToString();
				HeaderText = dr[1].ToString();
				Width = dr[2].ToString();

				DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
				TextCol.MappingName = MappingName ;
				TextCol.HeaderText = HeaderText ;
				TextCol.Width = System.Convert.ToInt32(Width);

				ts1.GridColumnStyles.Add(TextCol);
				//MListView.Columns.Add(ColumnText,System.Convert.ToInt32(ColumnWidth),System.Windows.Forms.HorizontalAlignment.Center);
			}

			
		

			dataGrid.TableStyles.Add(ts1);
			return true;		
		}

	}
}
